package com.hmall.search.mq;

import com.hmall.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HmallListener {

    @Autowired
    private SearchService searchService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "hmall.save.queue"),
            exchange = @Exchange(name = "hmall.topic", type = ExchangeTypes.TOPIC),
            key = "hmall.save"
    ))
    public void save(Long id) {
        if (id != null) {
            searchService.save(id);
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "hmall.delete.queue"),
            exchange = @Exchange(name = "hmall.topic",type = ExchangeTypes.TOPIC),
            key = "hmall.delete"
    ))
    public void delete(Long id) {
        if (id != null) {
            searchService.delete(id);
        }
    }
}
